package Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import utils.DBUtils;
import bean.CommentBody;
import bean.QueryMessageResult;

public class QueryMsgDao {
	private DBUtils dbUtils;
	public QueryMsgDao(){
		dbUtils=DBUtils.getInstance();
	}
	public QueryMessageResult getMessage(int userid) {
		
		QueryMessageResult queryMessageResult=new QueryMessageResult();
		int status=-1;
		dbUtils.getConnection();
		String sql=null;
		
		sql="select mb.from_id,mb.type,umb.user_mark,upb.user_custom_name,upb.user_phonenumber,"
				+ "upb.user_profile_str,ub.user_name,ulb.user_location from message_tb mb,user_marks_tb umb,"
				+ "user_profile_tb upb,user_tb ub,user_location_tb ulb where mb.from_id=umb.user_id and"
				+ " mb.from_id=upb.user_id and ub.user_id=mb.from_id and ulb.user_id=mb.from_id and to_id= ? ";
		try{
			List<Object> params=new ArrayList<Object>();
			params.add(userid);
			Map<String,Object> map=new HashMap<String,Object>();
			map=dbUtils.queryOneMap(sql, params);
			if(map.size()>0||!map.isEmpty()){
				queryMessageResult.setFrom_id(Integer.valueOf(map.get("from_id").toString()));
				queryMessageResult.setType(Integer.valueOf(map.get("type").toString()));
				if(map.get("user_custom_name")!=null){
					queryMessageResult.setAlias(map.get("user_custom_name").toString());
				}else{
					queryMessageResult.setAlias(map.get("user_name").toString());
				}
				queryMessageResult.setPhoneNumber(map.get("user_phonenumber").toString());
				queryMessageResult.setLocationId(map.get("user_location").toString());
				queryMessageResult.setBaseMark(map.get("user_mark").toString());
				queryMessageResult.setProfile(map.get("user_profile_str").toString());
				sql="select cb.comment_str,cb.mark,ub.user_name,upb.user_custom_name"
						+ " from comment_tb cb,user_tb ub,user_profile_tb upb "
						+ "where cb.from_id=ub.user_id and cb.from_id=upb.user_id and to_id= ? ";
				List<Map<String,Object>> listMap=new ArrayList<Map<String,Object>>();
				params.clear();
				params.add(queryMessageResult.getFrom_id());
				listMap=dbUtils.queryMultMap(sql, params);
				List<CommentBody> commentList=new ArrayList<CommentBody>();
				for(int i=0;i<listMap.size();i++){
					CommentBody body=new CommentBody();
					body.setComment(listMap.get(i).get("comment_str").toString());
					body.setMarks(listMap.get(i).get("mark").toString());
					if(listMap.get(i).get("user_custom_name")==null){
						body.setFromAlias(listMap.get(i).get("user_name").toString());
					}else{
						body.setFromAlias(listMap.get(i).get("user_custom_name").toString());
					}
					commentList.add(body);
					
				}
				queryMessageResult.setCommentList(commentList);
				
				params.clear();
				params.add(userid);
				sql="delete from message_tb where to_id=?";
				dbUtils.updateOrDelSQL(sql, params);
				status=1;
			}
			else{
				status=0;
			}
		}
		
		catch(SQLException e){
			status=-1;
			e.printStackTrace();
		}
		
		queryMessageResult.setStatus(status);
		return queryMessageResult;
	}

}
